//
//  ST_PinterestHomeCellModel.swift
//  btvideo
//
//  Created by mac on 2019/12/25.
//  Copyright © 2019 JarvanZhang. All rights reserved.
//

import UIKit
import SwiftyJSON
import SwifterSwift


var ST_PinterestHomeCellImageWidth:CGFloat = { return (ScreenWidth - 2 * 10 - ST_PinterestHomeCellMinimumColumnSpacing)/2.0 }()

var ST_PinterestHomeCellMinimumColumnSpacing = 15.cgFloat

class ST_PinterestHomeCellModel: NSObject {

    enum cellType:Int {
        case image = 0,video,Guidance_特质,Guidance_照片,Guidance_自我介绍,Guidance_个人资料
    }
    
    var guidanceImageName = ""
    
    var accid = ""
    
    var type = cellType.image
    
    var resourceList = [ST_PairsShareParticularModel]()
    
    var sign = ""
    
    var age = 18
    
    var nickname = ""
    
    var address = ""
    
    var is_new_user = false
    
//    真人
    var faceIdentified = false
    
    var lovedMe = false
    
    var showedMe = false
    
    var isProps = false
    
    var gender = 0
    
//   MARK: - UI properties
    let contentWidth = (ScreenWidth - 2 * 10 - 15)/2.0 - 13
    
    var content_attr:NSAttributedString?
    
    var firstImageSize = CGSize.zero
    
    var size = CGSize.zero
    
    override init() {
        super.init()
    }
    
    /**
     从后台的数据结构解析为本身数据结构
     */
    init(json:JSON) {
        
        super.init()
        
        accid = json["accid"].stringValue
        
        lovedMe = json["is_like_me"].boolValue
        
        showedMe = json["is_look_me"].boolValue
        
        isProps = json["is_props"].boolValue
        
        
        gender = json["gender"].intValue
        
        let detailArray = json["detail"].arrayValue
        
        guard let firstItem = detailArray.first else{return}

        //        类型(1:视频,2:图片)
        if firstItem["video_url"].stringValue != ""{
            type = .video
        }
        
        let modelArray = ST_PairsShareParticularModel.describe(json_array: detailArray)
        
        resourceList = modelArray
        
        sign = json["sign"].stringValue
        
//        sign = "生活在乡村田野的可爱飨灵。喜欢音乐与歌唱，但没有听众。"
        
        age = json["age"].intValue
        
        nickname = json["nickname"].stringValue
        
        address = json["address"].stringValue
        
        is_new_user = json["is_new_user"].boolValue
        
        faceIdentified = json["face_audit_state"].intValue == 3
//        is_new_user = Bool.random()
        
        create_view_model_set()
    }
    
    
    /**
     个人主页 推荐
     */
    init(recommendJson:JSON) {
            
        super.init()
        
        accid = recommendJson["accid"].stringValue
        
       

        //        类型(1:视频,2:图片)
        if recommendJson["video_url"].stringValue != ""{
            type = .video
        }
        
        let modelArray = ST_PairsShareParticularModel.describe(json_array: [recommendJson])
        
        resourceList = modelArray
        
        sign = recommendJson["sign"].stringValue
        
        age = recommendJson["age"].intValue
        
        is_new_user = recommendJson["is_new_user"].boolValue
        
        create_view_model_set()
    }
    
    
    static func describe(json_array:[JSON])->[ST_PinterestHomeCellModel]{
        
        let array = json_array.map { (json) -> ST_PinterestHomeCellModel in
            return ST_PinterestHomeCellModel.init(json: json)
        }
        
        return array
    }
    
}


extension ST_PinterestHomeCellModel{
    
    
    fileprivate func create_view_model_set(){
        
        var content_height = 0.cgFloat
        
        let titleHeight = 16.cgFloat
        
        if !sign.isEmpty{
            
            let attrString = NSMutableAttributedString(string: sign)
            
            let attr: [NSAttributedString.Key : Any] = [.font: UIFont.systemFont(ofSize: 12),.foregroundColor: UIColor(red: 0.6, green: 0.6, blue: 0.6, alpha: 1)]
            
            attrString.addAttributes(attr, range: NSRange(location: 0, length: attrString.length))
            
            content_attr = attrString
            
            content_height = attrString.height(withConstrainedWidth: contentWidth.int.cgFloat)
            
            content_height += 5
        }
        
        
        
        
        if let first = resourceList.first{
            
//            print("22222----222-----",ST_PinterestHomeCellImageWidth)
            let w = first.width < 1 ? 1 : first.width
            let h = ST_PinterestHomeCellImageWidth/w*first.height
            
            firstImageSize = CGSize.init(width: ST_PinterestHomeCellImageWidth.int.cgFloat, height: h.int.cgFloat)
        }
        
        
//        print("22222----222-----size:",firstImageSize)
        
        size = CGSize.init(width: ST_PinterestHomeCellImageWidth, height: 10 + titleHeight + content_height + firstImageSize.height )
        
//        print("Bug 00088888:",size)
        
    }
    
}



extension ST_PinterestHomeCellModel{

    /**
     单人的单个特质的所有图片 、视频
     */
    convenience init(singlePeculiarityJson:JSON) {
        self.init()
        
        resourceList = ST_PairsShareParticularModel.describe(json_array: [singlePeculiarityJson])
        
        //        类型(1:视频,2:图片)
        if resourceList.first?.type == .video{
            type = .video
        }
    }
    
}

